<template>
  <div class="container">
    <div class="login-container">
      <div class="leftBox">
        <div class="headerBox">
          <!-- <img :src="loginIcon" />
          智能政务可信问答系统 -->
        </div>
      </div>
      <div class="rightBox">
        <div class="tit Tip">用户注册</div>
        <div class="tit">请使用个人真实信息完成注册</div>
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-position="top"
        >
          <el-form-item label="手机号码" prop="account">
            <el-input
              class="inputDeep"
              type="text"
              v-model.trim="ruleForm.account"
              autocomplete="off"
              placeholder="请输入手机号"
              maxlength="11"
              @input="ruleForm.account = ruleForm.account.replace(/[^\d]/g, '')"
            ></el-input>
          </el-form-item>
          <el-form-item label="用户名" prop="name">
            <el-input
              class="inputDeep"
              type="text"
              v-model.trim="ruleForm.name"
              autocomplete="off"
              placeholder="请尽量使用真实姓名进行注册"
              maxlength="10"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              class="inputDeep"
              type="password"
              v-model.trim="ruleForm.password"
              autocomplete="off"
              placeholder="8位以上大写字母、小写字母、数字组成"
              show-password
              maxlength="50"
            ></el-input>
          </el-form-item>

          <el-form-item label="重复密码" prop="passwordRepeat">
            <el-input
              class="inputDeep"
              type="password"
              v-model.trim="ruleForm.passwordRepeat"
              autocomplete="off"
              placeholder="请重复输入登录密码"
              show-password
              maxlength="50"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              class="customBtn"
              type="primary"
              @click="submitForm('ruleForm')"
              >立即注册</el-button
            >
          </el-form-item>
          <div class="notRegister">
            已有账号？去 <span @click="loginFun">登录</span>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { userRegsiter } from "@/api/user.js";
export default {
  name: "register",
  data() {
    var number = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入手机号"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入登录密码"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        account: "",
        name: "",
        password: "",
        passwordRepeat: "",
      },
      rules: {
        account: [
          { validator: number, trigger: "blur", required: true },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入合法手机号",
            trigger: "blur",
          },
        ],
        name: [{ required: true, message: "请输入用户名称", trigger: "blur" }],
        password: [
          { required: true, validator: validatePass, trigger: "blur" },
          // {
          //   pattern: /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).{8,}$/,
          //   message: "密码由8位以上大写字母，小写字母和数字组成",
          //   trigger: "blur",
          // },
        ],
        passwordRepeat: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    // this.$set(
    //   this.ruleForm,
    //   "tenant_id",
    //   JSON.parse(sessionStorage.getItem("id"))
    // );
    // console.log(this.ruleForm, "rulefo");
  },
  methods: {
    loginFun() {
      this.$router.push("/login");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          userRegsiter(this.ruleForm).then((res) => {
            if (res.code == 200 && res.data) {
              this.$message.success('注册成功，请登录');
              this.$router.push({ path: "/login" });
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
        }
      });
    },
  },
};
</script>


<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background: rgb(243, 244, 245);
  display: flex;
  align-items: center;
  // overflow-x: scroll;
  background: url(@/assets/img/bg1.png);
  background: no-repeat;
}

.historyBox {
  margin-right: -40px;
}

.login-container {
  height: 721px;
  width: 1215px;
  overflow: hidden;
  margin: 0 auto;
  text-align: left;
  background: rgb(255, 255, 255);
  border-radius: 20px;
  box-shadow: 0px 0px 10px rgba(70, 70, 70, 0.25);
  display: flex;
  justify-content: space-between;
  .leftBox {
    width: 405px;
    // background: url("~@/assets/img/bg2.png") center center no-repeat;
    background-image: url("/src/assets/img/bg2.png");
    background-size: 100% 100%;
    .headerBox {
      display: flex;
      align-items: center;
      padding: 35px 0px 0px 25px;
      box-sizing: border-box;
      img {
        margin-right: 10px;
      }
    }
  }
  .rightBox {
    flex: 1;
    padding: 0px 240px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    box-sizing: border-box;
    text-align: left;
  }
  .tit {
    margin: 10px 0px;
    color: rgba(1, 1, 1, 0.8);
    font-size: 18px;
    width: 100%;
    text-align: left;
  }

  .Tip {
    font-size: 24px;
    font-weight: 700;
  }

  .customBtn {
    width: 328px;
    height: 49px;
    text-align: center;
    color: #fff;
    margin-top: 20px;
    background: linear-gradient(
      138.98deg,
      rgb(124, 161, 236) 7.549%,
      rgb(56, 112, 227) 95.507%
    );
    border-radius: 10px;
  }

  .notRegister {
    font-size: 14px;
    color: rgba(1, 1, 1, 0.5);
    span {
      color: rgb(56, 112, 227);
      cursor: pointer;
    }
  }
}
</style>
